package com.github.pig.training.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.UserVO;
import com.github.pig.training.entity.TrainingStudent;
import com.github.pig.training.entity.TrainingStudentRegister;
import com.github.pig.training.mapper.TrainingStudentRegisterMapper;
import com.github.pig.training.service.ITrainingStudentRegisterService;
import com.github.pig.training.service.ITrainingStudentService;
import com.github.pig.training.util.Assemble;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author andrew
 * @since 2018-06-26
 */
@Service
public class TrainingStudentRegisterServiceImpl extends ServiceImpl<TrainingStudentRegisterMapper, TrainingStudentRegister> implements ITrainingStudentRegisterService {

    @Autowired
    private ITrainingStudentService studentService;

    @Autowired
    private SysUserService userService;

    /**
     * 添加培训学员单据数据
     *
     * @param map
     * @return
     */
    @Override
    public R<Boolean> signUp(Map<String, String> map, UserVO user) {
        UserVO userVO = userService.findUserByUsername(user.getUsername());
        Assemble<TrainingStudent> studentAssemble = new Assemble<>();
        Assemble<TrainingStudentRegister> registerAssemble = new Assemble<>();
        TrainingStudent student = null;
        TrainingStudentRegister register = null;
        try {
            student = studentAssemble.assmbleObj(new TrainingStudent(),map);
            student.setCreateTime(new Date());
            student.setUpdateTime(new Date());
            student.setBizStatus("2202");
            student.setSourceOfChannel("2502");
            student.setFollowUpStatus("1603");
            student.setCallCount(0);
            student.setCurrentCallCount(0);
            student.setRegisterDate(new Date());
            student.setDelFlag(CommonConstant.STATUS_NORMAL);
            studentService.insert(student);
            EntityWrapper<TrainingStudent> nw = new EntityWrapper<>();
            nw.setSqlSelect("fid").where("id_card={0}",student.getIdCard());
            TrainingStudent t = studentService.selectOne(nw);
            register = registerAssemble.assmbleObj(new TrainingStudentRegister(),map);
            register.setStudentId(t.getFid());
            register.setCreateTime(new Date());
            register.setUpdateTime(new Date());
            register.setDelFlag(CommonConstant.STATUS_NORMAL);
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }
        return new R<>(super.insert(register));
    }

    /**
     * 添加培训学员单据数据
     *
     * @param trainingStudentRegister
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public R<Boolean> signUpDocuments(TrainingStudentRegister trainingStudentRegister){
        EntityWrapper<TrainingStudent> nw = new EntityWrapper<>();
        nw.where("fid={0}",trainingStudentRegister.getStudentId());
        TrainingStudent t = studentService.selectOne(nw);
        if(!(Integer.valueOf(StringUtils.isEmpty(t.getBizStatus())?"0":t.getBizStatus())>2201)){
            t.setSourceOfChannel("2501");
            t.setUpdateTime(new Date());
            t.setBizStatus("2202");
//            t.setFollowUpStatus("1603");
            t.setCallCount(0);
            t.setCurrentCallCount(0);
            t.setFollowUpUser(trainingStudentRegister.getFid());
            t.setRegisterDate(new Date());
            t.setDelFlag(CommonConstant.STATUS_NORMAL);
            studentService.updateById(t);
            trainingStudentRegister.setFid(null);
            insert(trainingStudentRegister);
            return new R<>(Boolean.TRUE);
        }
        return new R<>();
    }
}
